package com.origon.managedbeans;

import java.util.List;
import java.util.logging.Logger;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import com.origon.entities.Account;
import com.origon.entities.Address;
import com.origon.entities.Category;
import com.origon.entities.Country;
import com.origon.entities.Currency;
import com.origon.entities.Keeper;
import com.origon.entities.Shop;
import com.origon.entities.User;
import com.origon360.Services.ShopService;
import com.origon360.utilities.NavigationHelper;
import com.origon360.utilities.SessionUtil;

@ManagedBean(name = "shopBean")
@SessionScoped
public class ShopBean {
	
	private static Logger logger = Logger.getLogger(ShopBean.class.getName());

	@EJB
	private ShopService shopService;
	
	private boolean sameAddress;
	
	private Shop shop = new Shop();
	
	private Keeper keeper = new Keeper();
	
	private Account account = new Account();
	
	private User user = new User();
	
	private List<Currency> currencyList;
	
	private List<Category> categoryList;
	
	private List<Country> countryList;
	
	private List<Shop> shopList;
	
		
	public ShopBean(){
		init();
	}
	
	public void init(){
		shop.setKeeper(new Keeper());
		shop.setBillingAddress(new Address());
		shop.setShippingAddress(new Address());
		shop.setAccount(account);
		
	}
	
	public boolean isSameAddress() {
		return sameAddress;
	}

	public void setSameAddress(boolean sameAddress) {
		this.sameAddress = sameAddress;
	}

	public Shop getShop() {
		return shop;
	}

	public void setShop(Shop shop) {
		this.shop = shop;
	}


	public Keeper getKeeper() {
		return keeper;
	}


	public void setKeeper(Keeper keeper) {
		this.keeper = keeper;
	}

	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<Currency> getCurrencyList() {
		if(currencyList == null)
		{
			currencyList = shopService.fetchCurrencies();
		}
		return currencyList;
	}

	public void setCurrencyList(List<Currency> currencyList) {
		this.currencyList = currencyList;
	}


	public List<Category> getCategoryList() {
		if(categoryList == null)
		{
			categoryList = shopService.fetchCategories();
		}
		return categoryList;
	}



	public void setCategoryList(List<Category> categoryList) {
		this.categoryList = categoryList;
	}



	public List<Country> getCountryList() {
			if(countryList == null)
			{
				countryList = shopService.fetchCountries();
			}
		return countryList;
	}



	public void setCountryList(List<Country> countryList) {
		this.countryList = countryList;
	}


	public List<Shop> getShopList() {
		if(shopList == null)
		{
			shopList = shopService.fetchShops();
		}
		return shopList;
	}

	public void setShopList(List<Shop> shopList) {
		this.shopList = shopList;
	}

	public String navigateToCreateShop(){
		return NavigationHelper.CREATE_SHOP_URI;
		
	}
	
	public String navigateToShops(){
		return NavigationHelper.SHOW_SHOPS_URI;
		
	}
	
	public String navigateToShop(Shop shop){
		if(SessionUtil.getSessionUser() == null)
		{
			return NavigationHelper.HOME_URI;
		}
		else
		{
			return NavigationHelper.SHOW_SHOP_URI;
		}		
	}
	
	public String navigateToProducts(){
		return NavigationHelper.PRODUCTS_URI;
		
	}
	
	public void addMessage(String msg){
		FacesMessage facesMessage = new FacesMessage(msg);
		FacesContext.getCurrentInstance().addMessage(null, facesMessage);
	}
	
	public String createShop(){
		
		try {
			
			if(sameAddress)
			{
				shop.setShippingAddress(shop.getBillingAddress());
			}
			User user = SessionUtil.getSessionUser();
			shop.setUser(user);
			
			shopService.createShop(shop);
			
			String message = "Shop was successfully Created";
			addMessage(message);
			
		} catch (Exception e) {
			String message = "Shop creation Failed";
			addMessage(message);
		}
		
		return null;
	}

	
}
